package com.pjb.springbootjjwt.store;

import com.pjb.springbootjjwt.store.entity.Define;
import com.pjb.springbootjjwt.store.entity.Line;
import com.pjb.springbootjjwt.store.utils.FileUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;


public class File {

    static File file = new File();
    static ExecutorService executorService = Executors.newFixedThreadPool(8);
    static Map<String, BlockingQueue<Line>> container = new HashMap<>();

    public static File getInstance() {
        return file;
    }

    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("key", "9999");
        String c = FileUtil.jsonStr(map);
        System.out.println("c:" + c);
        Map map2 = FileUtil.toJson(c, Map.class);
        System.out.println("c:" + map2);
        String table = "vv";
        List<Line> list = Defines.getHandler(table).get(table).getLine();

        for (Line line : list) {
            System.out.println(line.toText(Defines.fieldsLength(table)));
        }
    }

    /**
     * 获取ddl内容字符串
     *
     * @param name
     * @return
     * @throws IOException
     */
    public String getDDL(String name) throws IOException {
        String j = FileUtil.read(name + ".frm");
        Define d = FileUtil.toJson(j, Define.class);
        return d.getDdl();
    }

    /**
     * 新增记录到表
     *
     * @param table
     * @param line
     * @throws Exception
     */
    public void add(String table, Line line) throws Exception {
        if (!container.containsKey(table)) {
            container.put(table, new ArrayBlockingQueue<Line>(500));
        }

        Future future = executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                BlockingQueue<Line> q = container.get(table);
                //saveOne(t, l.take());
                List<Line> list = new ArrayList();
                while (q.size() > 0) {
                    list.add(q.take());
                }

                Defines.getHandler(table).saveList(table, list);
                return null;
            }
        });

        BlockingQueue list = container.get(table);
        list.put(line);
        future.get();

    }

    /**
     * 更新记录到表
     *
     * @param table
     * @param list
     * @param sub
     * @throws IOException
     */
    public void update(String table, List<Line> list, List<Line> sub) throws IOException {
        for (Line s : sub) {
            for (Line l : list) {
                if (l.getKey().equals(s.getKey())) {
                    l.setData(s.getData());
                }
            }
        }
        save(table, list);
    }

    /**
     * 保存记录到表
     *
     * @param table
     * @param sub
     * @throws IOException
     */
    private void save(String table, List<Line> sub) throws IOException {
        String c = "";
//        for (Line line: list) {
//            c += line.toText(Defines.fieldsLength(table)) + ";\r\n";
//        }
//        FileUtil.write(FileUtil.getPath(table), c);

//        for (Line line: list) { //处理主键
//            c += line.getKey() + ";\r\n";
//        }
//        FileUtil.write(FileUtil.getPath(table) + ".idx", c);
    }

    /**
     * 查询一张表的索引
     *
     * @param table
     * @return
     * @throws IOException
     */
    public boolean getIndex(String table, String key) throws IOException {
        String c = FileUtil.read(table + ".idx");
        return c.indexOf(key) >= 0;
    }
}
